/**
 * Created by lghdb on 2018/7/10.
 */

var conf = require('../config');
var api = require('./http');

var urlPrefix = conf.get("docker_reg_api");

function getUrl(action) {
    return `${urlPrefix}/${action}`;
}

async function catalog() {
    var value = await api.get(getUrl("_catalog"));
    return JSON.parse(value.data).repositories
}

async function tags(image) {
    var url = getUrl(`${image}/tags/list`);
    var value = await api.get(url);
    return JSON.parse(value.data).tags;
}

async function images() {
    var result = [];
    var images = await catalog();
    for(var i in images) {
        var img = images[i];
        var _tags = await tags(img);
        for (var t in _tags){
            var tag = _tags[t];
            result.push({
                name:img,
                tag: tag
            });
        }
    }
    return result;
}

async function imageInfo(image, tag) {
    var value = await api.get(getUrl(`${image}/manifests/${tag}`));
    return JSON.parse(value.data);
}

module.exports = {
    catalog : catalog,
    tags: tags,
    images: images,
    imageInfo: imageInfo
}